package com.project.petLossSystem.controller.admin;

import com.project.petLossSystem.service.PetPostService;
import com.project.petLossSystem.util.PageQueryUtil;
import com.project.petLossSystem.util.Result;
import com.project.petLossSystem.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.NumberUtils;

import java.util.Map;

@Controller
@RequestMapping("/admin")
public class PetPostController {
    @Autowired
    private PetPostService petPostService;

    @GetMapping("/petPosts")
    public String petPost(){
        return "/pc/admin/pet_posts";
    }

    @GetMapping("/petPosts/list")
    @ResponseBody
    public Result list(@RequestParam Map<String, Object> params){
        if (ObjectUtils.isEmpty(params.get("page")) || ObjectUtils.isEmpty(params.get("rows"))) {
            return ResultGenerator.failureResult("参数异常！");
        }
        // 判断userId是否有效
        if(!ObjectUtils.isEmpty(params.get("userId"))){
            Long userId = Long.valueOf((String) params.get("userId"));
            params.put("userId", userId);
        }
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        return ResultGenerator.successResult(petPostService.getPostPage(pageQueryUtil));
    }

    @PutMapping("/petPosts/delete")
    @ResponseBody
    public Result delete(@RequestBody Long[] postIds){
        if(postIds.length < 1){
            return ResultGenerator.failureResult("参数异常！");
        }
        if(petPostService.deletePosts(postIds)){
            return ResultGenerator.successResult();
        }else return ResultGenerator.failureResult("删除失败！");
    }
}
